#include "StdAfx.h"
#include "SDLDevice.h"

SDLDevice::SDLDevice(void)
{
	m_width	= 640;
	m_height = 480;
	m_depth = 32;
	m_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
	m_running = false;
	m_display = NULL;
}

SDLDevice::SDLDevice( int width,int height,int depth,Uint32 flags )
{
	m_width = width;
	m_height = height;
	m_depth = depth;
	m_flags = flags;
}
SDLDevice::~SDLDevice(void)
{
}

int SDLDevice::OnExecute()
{
	if (OnInit()==false)
	{
		return -1;
	}
	m_running = true;
	SDL_Event Event;
	while(m_running)
	{
		while(SDL_PollEvent(&Event))
		{
			OnEvent(&Event);
		}
		OnLoop();
		OnRender();
	}
	OnCleanup();
	return 0;
}

bool SDLDevice::OnInit()
{
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)<0)
	{
		return false;
	}
	if ((m_display=SDL_SetVideoMode(m_width,m_height,m_depth,m_flags))=NULL)
	{
		return false;
	}
	return true;
}

void SDLDevice::OnEvent( SDL_Event* event )
{
	if (event->type == SDL_QUIT)
	{
		m_running = false;
	}
}

void SDLDevice::OnLoop()
{

}

void SDLDevice::OnRender()
{

}

void SDLDevice::OnCleanup()
{
	SDL_FreeSurface(m_display);
	SDL_Quit();
}